function check_add_hdtt(id_row,add,tien_sua,idsua,datt){
	sotien=document.getElementById('sotientt_'+id_row);
	sotien_=document.getElementById('sotientt_'+id_row+'_');
	start = document.getElementById('ngaytt_'+id_row);
	end = document.getElementById('ngaytt_tt_'+id_row);
	var view = COREJS._getJsLang('view');
	var Hdo_Id = COREJS._getJsLang('Hdo_Id');
	if(sotien_.value==''){
		alert(COREJS._getJsLang('_BAN_CHUA_NHAP_SO_TIEN_TT'));
		sotien.focus();
		return false;
	}
	if(sotien_.value<=0){
		alert(COREJS._getJsLang('_SO_TIEN_TT_PHAI_DUONG'));
		sotien.focus();
		return false;
	}
	
	if(COREJS._getJsLang('DINH_DANG') == 1){
		var dinhdang = '/';
	}
	else if(COREJS._getJsLang('DINH_DANG') == 2){
		var dinhdang = '-';
	}
	else{
		var dinhdang = '.';
	}
	if(end.value!=""){
		Ngay_Bat_Dau= (start.value).split(dinhdang);
		ngay_bd=Ngay_Bat_Dau[0];
		thang_bd=Ngay_Bat_Dau[1];
		nam_bd=Ngay_Bat_Dau[2];
		Ngay_KT= (end.value).split(dinhdang);
		ngay_kt=Ngay_KT[0];
		thang_kt=Ngay_KT[1];
		nam_kt=Ngay_KT[2];
		if(nam_bd*1 > nam_kt*1){
			alert(COREJS._getJsLang('_ALERT_NGAY_TT_TT_LONHON_NGAY_CHI'));
			end.focus();
			return false;
		}
		if(nam_bd*1 == nam_kt*1){
			if(thang_bd*1 > thang_kt*1){
				alert(COREJS._getJsLang('_ALERT_NGAY_TT_TT_LONHON_NGAY_CHI'));
				end.focus();
				return false;
			}
			if(thang_bd*1 == thang_kt*1){
				if(ngay_bd*1 > ngay_kt*1){
					alert(COREJS._getJsLang('_ALERT_NGAY_TT_TT_LONHON_NGAY_CHI'));
					end.focus();
					return false;
				}
			}
		}
		
		//kiem tra ngay thanh toan phai <= ngay hien tai
		
	}
	if(document.getElementById('Da_TT_'+id_row).checked==true && end.value==""){
			alert(COREJS._getJsLang('_BAN_CHUA_CHON_NGAY_TT_THUC_TE'));
			end.focus();
			return false;
	}
	else{
		//jQuery("#form_dau_hdtt_"+id_row).remove();
		//jQuery("#form_cuoi_hdtt_"+id_row).remove();
		var gia_tri=document.getElementById('giatri_').value;
		var tientt=sotien_.value;
		var tien_da_tt_cu=document.getElementById('tiendatt_').value;
		if(document.getElementById('Da_TT_'+id_row).checked==true){
			var co_check=1;
		}
		else{
			var co_check=0;
		}
		if(datt=='add'){
			if(co_check==1){
				//tinh them tien
				var tien_da_tt=tien_da_tt_cu*1+tientt*1-tien_sua*1;
			}
			else{
				//tinh them tien
				var tien_da_tt=tien_da_tt_cu;
			}
		}
		else{
			//truong hop check thanh toan va sua van check
			if(co_check==1 && datt==1){
				//tinh them tien
				var tien_da_tt=tien_da_tt_cu*1+tientt*1-tien_sua*1;
			}
			//truong hop check thanh toan nhung truoc ko check
			if(co_check==1 && datt==0){
				//tinh them tien
				var tien_da_tt=tien_da_tt_cu*1+tientt*1;
			}
			//truong hop h khong check nhung truoc co check
			if(co_check==0 && datt==1){
				var tien_da_tt=tien_da_tt_cu*1-tien_sua*1;
			}
			if(co_check==0 && datt==0){
				var tien_da_tt=tien_da_tt_cu;
			}
		}
		//tien da thanh toan
		document.getElementById('tiendatt_').value=tien_da_tt;
		//tien chua thanh toan
		tien_chua_tt=gia_tri*1-tien_da_tt*1;
	
		document.getElementById('tienphaitt_').value=tien_chua_tt;
		if(view == 1){
			document.getElementById('tienphaitt').innerHTML=formatNumber_DB(tien_chua_tt);
			document.getElementById('tiendatt').innerHTML=formatNumber_DB(tien_da_tt);
		}
		else{
			document.getElementById('tienphaitt').value=formatNumber_DB(tien_chua_tt);
			document.getElementById('tiendatt').value=formatNumber_DB(tien_da_tt);
		}
		
		
		if(id_row!=0){
			var getFormValues = $('#frm_popup_hdtt_'+id_row).serialize();
			var urlaction = "modules/contract/add/AddHdtt.php?id_row="+id_row+'&add='+add+'&'+getFormValues+'&tien_da_tt='+tien_da_tt+'&hdt_id='+idsua+'&Hdo_Id='+Hdo_Id+'&tien_chua_tt='+tien_chua_tt;
			$.getJSON(urlaction,null,function(data){
				document.getElementById('row_suahdtt_'+id_row).innerHTML = data;
			});
			$(".dialogThanhToan_"+id_row ).dialog( "close" );
			jQuery("#dialogThanhToan_"+id_row).remove();
		}
		else{
			var getFormValues = $('#frmAddHdtt').serialize();
			var urlaction = "modules/contract/AddHdtt.php?id_row="+id_row+'&add='+add+'&'+getFormValues+'&add='+add+'&tien_da_tt='+tien_da_tt+'&hdt_id='+idsua+'&Hdo_Id='+Hdo_Id+'&tien_chua_tt='+tien_chua_tt;
			$.getJSON(urlaction,null,function(data){
				$(data).appendTo("#divrowhopdongthanhtoan");
			});
			$("#dialogAddHDTT").dialog("close");
			//reset 
			document.getElementById('sotientt_0_').value='';
			document.getElementById('sotientt_0').value='';
			document.getElementById('gc_thanh_toan_0').value='';
			document.getElementById('ngaytt_tt_0').value='';
		}
		document.getElementById('hopdongtt').style.display='none';
	}
}
function check_unckeck_hdtt(id,date){
	if(document.getElementById('Da_TT_'+id).checked==true){
		document.getElementById('ngaytt_tt_'+id).value=date;
	}
	else{
		document.getElementById('ngaytt_tt_'+id).value='';
	}
}

function delete_hdtt(id_row,id,tien_xoa,datt){
	so_hdtt_ht=document.getElementById('count_hdtt').value;
	jQuery("#row_suahdtt_"+id_row).remove();
	count_hdtt=so_hdtt_ht*1-1;
	document.getElementById('count_hdtt').value=count_hdtt;
	document.getElementById('display_count_hdtt').innerHTML=count_hdtt;
	tien_da_tt=document.getElementById('tiendatt_').value;
	tien_phai_tt=document.getElementById('tienphaitt_').value;
	gia_tri_hd=document.getElementById('giatri_').value;
	var view = COREJS._getJsLang('view');
	if(datt==1){
		//tien chua thanh toan
		tien_da_tt=tien_da_tt*1-tien_xoa*1;
		tien_phai_tt=gia_tri_hd*1-tien_da_tt*1;
		
	}
	
	document.getElementById('tienphaitt_').value=tien_phai_tt;
	document.getElementById('tiendatt_').value=tien_da_tt;
	if(id!=''){
		id_table=document.getElementById('id_table').value;
		var urlaction = COREJS._getJsLang('URL')+"/modules/hopdong/edit/deleteHDTT?id="+id+'&id_table='+id_table+'&tien_phai_tt='+tien_phai_tt+'&tien_da_tt='+tien_da_tt;
			$.getJSON(urlaction,null,function(data){
			});
	}
	if(view == 1){
		document.getElementById('tienphaitt').innerHTML=formatNumber_DB(tien_phai_tt);
		document.getElementById('tiendatt').innerHTML=formatNumber_DB(tien_da_tt);
	}
	else{
		document.getElementById('tienphaitt').value=formatNumber_DB(tien_phai_tt);
		document.getElementById('tiendatt').value=formatNumber_DB(tien_da_tt);
	}
	if(count_hdtt == 0){
		document.getElementById('hopdongtt').style.display='block';
	}
}